#include "Graphics/OpenGL/Win32/GLWin32Context.h"


namespace tyro
{
	namespace Graphics
	{
		GLWin32Context::GLWin32Context( HDC hDC, HGLRC hRC )
			: hDC(hDC)
			, hRC(hRC)
		{
		}

		GLWin32Context::~GLWin32Context()
		{

		}


		void GLWin32Context::Release()
		{
			if (hRC != NULL)
			{
				wglDeleteContext(hRC);
				hRC = NULL;
				hDC = NULL;
			}

		}

		GLContext* GLWin32Context::Clone() const
		{
			// Create new context from current
			HGLRC newHDC = wglCreateContext(hDC);

			
			if (!newHDC)
			{
				//TODO handle creation error
			}

			HGLRC oldHRC = wglGetCurrentContext();
			HDC oldHDC	= wglGetCurrentDC();

			wglMakeCurrent(NULL,NULL);

			if (!wglShareLists(hRC, newHDC))
			{
				//TODO handle share error
			}

			wglMakeCurrent(oldHDC, oldHRC);

			return new GLWin32Context(hDC, newHDC);
		}

		void GLWin32Context::MakeCurrent()
		{
			wglMakeCurrent(hDC, hRC);
		}

		void GLWin32Context::EndCurrent()
		{
			wglMakeCurrent(NULL , NULL);
		}

	}
}